//
//  StaffLetterSort.m
//  QRMaster
//
//  Created by lc on 16/5/18.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "StaffLetterSort.h"
#import "ChineseString.h"
#import "pinyin.h"
#import "StaffModel.h"
@implementation StaffLetterSort

+ (NSMutableArray*)getChinesArray:(NSArray*)originalArray{
    if (originalArray.count == 0 || originalArray == nil) {
        return nil;
    }
    int count = (int)originalArray.count;
    for(int i =0; i < count; i++)
    {
        //创建一个临时的数据模型对象
        ChineseString *chineseString=[[ChineseString alloc]init];
        //给模型赋值
        StaffModel * model = [originalArray objectAtIndex:i];
        NSString *pinYinResult = [NSString string];
        chineseString.string = model.userName;
        if(chineseString.string==nil)
        {
            chineseString.string=@"";
        }
        if(![chineseString.string isEqualToString:@""])
        {
            //join(链接) the pinYin (letter字母) 链接到首字母
            char FirstHeader = pinyinFirstLetter([chineseString.string characterAtIndex:0]);
            if ((FirstHeader < 91 && FirstHeader > 64)||(FirstHeader < 123 && FirstHeader > 96)) {
                pinYinResult = [pinYinResult stringByAppendingString:[[NSString stringWithFormat:@"%c",FirstHeader]uppercaseString]];
            }else {
                pinYinResult = @"#";
            }
        }
        model.headerWord = pinYinResult;
    }
    
    NSMutableArray * array = [NSMutableArray arrayWithArray:[originalArray sortedArrayUsingComparator:^NSComparisonResult(StaffModel * model1, StaffModel * model2) {
        return [model1.headerWord compare:model2.headerWord];
    }]];
    
    // "#"处理，放到数组最后
    array = [StaffLetterSort sort:array];

    return array;


}

+ (NSMutableArray *)letterSortArray:(NSArray *)originalArray{
    
    NSMutableArray *tempArray = [StaffLetterSort getChinesArray:originalArray];
    NSMutableArray *LetterResult = [NSMutableArray array];
    NSMutableArray *item = [NSMutableArray array];
    NSString *tempString;
    
    //拼音分组
    for (StaffModel * model in tempArray) {
        
        NSString *pinyin = model.headerWord;
        //不同
        if(![tempString isEqualToString:pinyin])
        {
            //分组
            item = [NSMutableArray array];
            [item  addObject:model];
            [LetterResult addObject:item];
            //遍历
            tempString = pinyin;
        }else//相同
        {
            [item  addObject:model];
        }
    }
    return LetterResult;

}

+ (NSMutableArray *)indexArray:(NSArray *)originalArray{
    NSMutableArray *tempArray = [StaffLetterSort getChinesArray:originalArray];
    NSMutableArray *A_Result = [NSMutableArray array];
    NSString *tempString ;
    for (StaffModel * model in tempArray)
    {
        NSString *pinyin = model.headerWord;
        //不同
        if(![tempString isEqualToString:pinyin])
        {
            [A_Result addObject:pinyin];
            tempString = pinyin;
        }
    }
    return A_Result;
    
}

#pragma mark - #的处理，放到数组最后
+ (NSMutableArray *)sort:(NSMutableArray *)array{
    NSInteger index = array.count;
    for (int i=0; i< index; i++) {
        StaffModel * model = [array objectAtIndex:i];
        if ([model.headerWord isEqualToString:@"#"]) {
            [array removeObject:model];
            [array addObject:model];
            index --;
            i--;
        }
    }
    return array;
}

@end
